/**
 * @Date 2023-02-16 15:51:10
 * @Remark block1
 */

// lib
import AES from "crypto-js/aes";
import enc from "crypto-js/enc-utf8";
// config
import { APP_SETTING } from "config/setting";
// interface

/**
 * 加密
 */
function encrypt<T>(data: T): string {
  const bytes = AES.encrypt(JSON.stringify(data), APP_SETTING.APP_ID);
  return bytes.toString();
}

/**
 * 解密
 */
function decrypt<T>(data: string): T {
  const bytes = AES.decrypt(data, APP_SETTING.APP_ID);
  return JSON.parse(enc.stringify(bytes));
}

/**
 * 加密URL字符串
 */
function encryptUrlParams<T>(data: T): string {
  const str = encrypt(data);
  return encodeURIComponent(str);
}

/**
 * 解密URL字符串
 */
function decryptUrlParams<T>(data: string): T {
  const str = decodeURIComponent(data);
  return decrypt(str);
}

const encryptUtils = {
  encrypt,
  decrypt,
  encryptUrlParams,
  decryptUrlParams,
};

export default encryptUtils;
